package com.system.controller;

import com.system.pojo.Product;
import com.system.pojo.Purchase;
import com.system.pojo.SysUser;
import com.system.service.PurchaseService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

@Controller
@ResponseBody
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public ResultVo<Purchase> insert (@RequestBody Product product){
        ResultVo<Purchase> resultVo = new ResultVo<Purchase>();
        try {
            resultVo = purchaseService.insert(product);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("进货失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/update/{purchaseId}")
    public ResultVo<Purchase> update (@PathVariable  Integer purchaseId, HttpSession session){
        ResultVo<Purchase> resultVo = new ResultVo<Purchase>();
        SysUser sysUser =(SysUser) session.getAttribute("sysUser");
        if (sysUser == null){
            resultVo.setMessage("进货失败");
            return resultVo;
        }
        try {
            resultVo = purchaseService.updateById(sysUser.getId(),purchaseId);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("进货失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/selectAllDone")
    public ResultVo<Purchase> selectAllDone (){
        ResultVo<Purchase> resultVo = new ResultVo<Purchase>();
        try{
            resultVo = purchaseService.selectAllDone();
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询进货单失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/selectAllUndone")
    public ResultVo<Purchase> selectAllUndone (){
//        System.out.println("controller方法");
        ResultVo<Purchase> resultVo = new ResultVo<Purchase>();
        try{
//            System.out.println("进来了");
            resultVo = purchaseService.selectAllUndone();
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询未完成进货单失败");
        }finally {
            return resultVo;
        }
    }
}
